﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;


using MINERP.COM;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;


namespace MINERP.DAL.CustomerManage
{
    public class OrderBagDAL : DALBase
    {

        private static ObjectQuery<Tb_OrderBag> GetOrderBagsByStatus(int status)
        {
            return DataEntites.Tb_OrderBag.Where(string.Format("it.status = {0}", status)).Include("Shop").Include("users").OrderBy("it.status");
        }

        /// <summary>
        /// 得到正在生产和未生产的订单列表
        /// </summary>
        /// <returns></returns>
        private static ObjectQuery<Tb_OrderBag> GetDefault()
        {
            DateTime defaultTime = DateTime.Now.AddDays(-30);
            return DataEntites.Tb_OrderBag.Where(string.Format("it.OrderTime > cast({0} as System.DateTime) ", "'" + defaultTime + "'")).Include("users").Include("Shop").OrderBy("it.status");
        }

        /// <summary>
        /// 获取所有的停止、恢复记录
        /// </summary>
        /// <returns></returns>
        public static List<CtmOperateRecord> GetStopRecords(int productId)
        {
            try
            {
                List<CtmOperateRecord> query = DataEntites.CtmOperateRecord.Where(p => p.orderInfo.OrderProductId == productId).ToList<CtmOperateRecord>();
                return query;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #region // 分页

        /// <summary>
        /// 得到默认的第一页的数据
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetDefaultFirstPageData(int count)
        {
            try
            {
                int delStatus = (int)COM.OrderBagStatus.Deleted;
                return DataEntites.Tb_OrderBag.Where(p => p.Status != delStatus).Take(count).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 得到指定的范围内的记录
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public static List<Tb_OrderBag> GetRequestPageData(int start, int end, int status)
        {
            //if (status == -1)
            //{
            //    return DataEntites.Tb_OrderBag.Where("true").OrderBy("it.BagCode desc").Include("users").Include("Shop").Top(end.ToString()).Skip("it.bagCode", start.ToString()).OrderByDescending(p => p.OrderBagId).ToList();
            //}
            ////return DataEntites.Tb_OrderBag.Where("it.status=" + status).OrderBy("it.BagCode desc").Include("users").Include("Shop").Top(end.ToString()).Skip("it.bagCode", start.ToString()).OrderByDescending(p => p.OrderBagId).ToList();
            //string bagSql = "select value it from Tb_OrderBag as it where it.status=" + status +
            //                "order by it.OrderBagId desc";
            //return DataEntites.CreateQuery<Tb_OrderBag>(bagSql).Include("users").Include("Shop").Skip("it.bagCode", start.ToString()).Top(end.ToString()).OrderByDescending(p => p.OrderBagId).ToList();

            int delStatus = (int)COM.OrderBagStatus.Deleted;
            int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
            List<Tb_OrderBag> orders = new List<Tb_OrderBag>();
            int orderNum = end - start;
            if (status == -1)
            {
                orders = DataEntites.Tb_OrderBag.Where(p => p.Status != delStatus && p.Status != autoStartOrder).OrderByDescending(o => o.OrderBagId).Skip(start).Take(orderNum).ToList();
            }
            else
            {
                orders = DataEntites.Tb_OrderBag.Where(p => p.Status == status && p.Status != delStatus).OrderByDescending(o => o.OrderBagId).Skip(start).Take(orderNum).ToList();
            }
            DataEntites.Refresh(RefreshMode.StoreWins, orders);
            return orders;
        }
        /// <summary>
        /// 得到指定的范围内的记录(备份)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public static List<Tb_OrderBag_BK> GetRequestPageData_bk(int start, int end)
        {
            List<Tb_OrderBag_BK> orders = new List<Tb_OrderBag_BK>();
            int orderNum = end - start;
            //从备份表中取数据，不需要任何状态条件(它们的状态全是完成)
            orders = DataEntites.Tb_OrderBag_BK.OrderByDescending(o => o.OrderBagId).Skip(start).Take(orderNum).ToList();
            DataEntites.Refresh(RefreshMode.StoreWins, orders);
            return orders;
        }

        /// <summary>
        /// 得到记录总数
        /// </summary>
        /// <returns></returns>
        public static int GetRecordCount(int status)
        {
            try
            {
                int delStatus = (int)COM.OrderBagStatus.Deleted;
                if (status == -1)
                    return DataEntites.Tb_OrderBag.Where(p => p.Status != delStatus).Count();
                return DataEntites.Tb_OrderBag.Where(p => p.Status == status && p.Status != delStatus).Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 得到记录总数(备份)
        /// </summary>
        /// <returns></returns>
        public static int GetRecordCount_bk()
        {
            try
            {
                 return DataEntites.Tb_OrderBag_BK.Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 得到记录的产品总数
        /// </summary>
        /// <returns></returns>
        public static int GetRecordProductCount(int orderStatus)
        {
            try
            {
                int delStatus = (int)COM.OrderBagStatus.Deleted;
                if (orderStatus == -1)
                    return DataEntites.orderInfo.Where(p => p.Tb_OrderBag.Status != delStatus).Count();
                return DataEntites.orderInfo.Where(p => p.Tb_OrderBag.Status == orderStatus && p.Tb_OrderBag.Status != delStatus).Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 得到记录的产品总数(备份)
        /// </summary>
        /// <returns></returns>
        public static int GetRecordProductCount_bk()
        {
            try
            {
                return DataEntites.orderInfo_BK.Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #endregion

        /// <summary>
        /// 根据CODE查询
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static ObjectQuery<Tb_OrderBag> GetOrderBagByCode(string code)
        {
            return DataEntites.Tb_OrderBag.Where(string.Format("it.BagCode = {0}", code)).Include("Shop").Include("users").OrderBy("it.status");
        }

        /// <summary>
        /// 通过ID得到对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagByID(int id)
        {
            ObjectQuery<Tb_OrderBag> list = DataEntites.Tb_OrderBag.Where(string.Format("it.OrderBagId = {0}", id)).Include("Shop").Include("users").OrderBy("it.status");

            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();
            foreach (Tb_OrderBag item in list)
            {
                collection.Add(item);
            }

            return collection;
        }
        /// <summary>
        /// 通过ID得到对象(备份)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag_BK> GetOrderBagByID_bk(int id)
        {
            ObjectQuery<Tb_OrderBag_BK> list = DataEntites.Tb_OrderBag_BK.Where(string.Format("it.OrderBagId = {0}", id)).Include("Shop").Include("users").OrderBy("it.status");

            List<Tb_OrderBag_BK> collection = new List<Tb_OrderBag_BK>();
            foreach (Tb_OrderBag_BK item in list)
            {
                collection.Add(item);
            }

            return collection;
        }

        /// <summary>
        /// 得到生产状态的订单
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagByStatus(int status)
        {
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            ObjectQuery<Tb_OrderBag> list = GetOrderBagsByStatus(status);

            foreach (Tb_OrderBag item in list)
            {
                collection.Add(item);
            }

            return collection;
        }

        /// <summary>
        /// 默认的查询
        /// </summary>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetDefaultOrderBag()
        {
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            ObjectQuery<Tb_OrderBag> list = GetDefault();

            foreach (Tb_OrderBag item in list)
            {
                collection.Add(item);
            }

            return collection;
        }

        /// <summary>
        /// 订单中的其中一张照片
        /// </summary>
        /// <param name="bagID"></param>
        /// <returns></returns>
        public static string GetOneImagePathByBag(int bagID)
        {
            int orderid = OrderInfoDAL.GetOneOrderInfoIDByBagID(bagID);

            if (orderid != 0)
            {
                return PhotoInfoDAL.GetOnePicPathByOrderInfoID(orderid);
            }
            else
            {
                return string.Empty;
            }
        }
        public static string GetOneImagePathByBag_bk(int bagID)
        {
            int orderid = OrderInfoDAL.GetOneOrderInfoIDByBagID_bk(bagID);

            if (orderid != 0)
            {
                return PhotoInfoDAL.GetOnePicPathByOrderInfoID_bk(orderid);
            }
            else
            {
                return string.Empty;
            }
        }

        public static DateTime GetPreCompleteTimeByOrderBag(int orderBag)
        {
            List<int> infoList = OrderInfoDAL.GetOrderInfoIDByOrderBagID(orderBag);

            DateTime preCompleteTime = DateTime.MinValue;

            if (infoList.Count > 0)
            {
                foreach (var item in infoList)
                {
                    DateTime temp = WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime(item);
                    if (preCompleteTime < temp)
                    {
                        preCompleteTime = temp;
                    }
                }
            }

            return preCompleteTime;
        }
        public static DateTime GetPreCompleteTimeByOrderBag_bk(int orderBag)
        {
            List<int> infoList = OrderInfoDAL.GetOrderInfoIDByOrderBagID_bk(orderBag);

            DateTime preCompleteTime = DateTime.MinValue;

            if (infoList.Count > 0)
            {
                foreach (var item in infoList)
                {
                    DateTime temp = WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime_bk(item);
                    if (preCompleteTime < temp)
                    {
                        preCompleteTime = temp;
                    }
                }
            }

            return preCompleteTime;
        }

        /// <summary>
        /// 通过SHOPID得到订单列表
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        private static List<Tb_OrderBag> GetOrderBagListByShopID(int shopid, DateTime start, DateTime end)
        {
            int delOrder = (int)COM.OrderBagStatus.Deleted;
            int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            string esql = "it.Shop.shopId = " + shopid + " and it.OrderTime >=  cast('" + start + "' as System.DateTime) and it.OrderTime < cast('" + end + "' as System.DateTime) and it.Status!=" + delOrder + " and it.Status!=" + autoStartOrder;
            //ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag.Where(string.Format("it.Shop.shopId = {0} and it.OrderTime >=  cast({1} as System.DateTime) and it.OrderTime < cast({2} as System.DateTime)", shopid,start,end)).Include("Shop").Include("users").OrderBy("it.status");
            ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag.Where(esql).Include("Shop").Include("users").OrderBy("it.status");

            collection.AddRange(objList);

            return collection;
        }
        /// <summary>
        /// 通过SHOPID得到订单列表(备份)
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        private static List<Tb_OrderBag_BK> GetOrderBagListByShopID_bk(int shopid, DateTime start, DateTime end)
        {
            int delOrder = (int)COM.OrderBagStatus.Deleted;
            int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
            List<Tb_OrderBag_BK> collection = new List<Tb_OrderBag_BK>();

            string esql = "it.Shop.shopId = " + shopid + " and it.OrderTime >=  cast('" + start + "' as System.DateTime) and it.OrderTime < cast('" + end + "' as System.DateTime) and it.Status!=" + delOrder + " and it.Status!=" + autoStartOrder;
            //ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag.Where(string.Format("it.Shop.shopId = {0} and it.OrderTime >=  cast({1} as System.DateTime) and it.OrderTime < cast({2} as System.DateTime)", shopid,start,end)).Include("Shop").Include("users").OrderBy("it.status");
            ObjectQuery<Tb_OrderBag_BK> objList = DataEntites.Tb_OrderBag_BK.Where(esql).Include("Shop").Include("users").OrderBy("it.status");

            collection.AddRange(objList);

            return collection;
        }

        /// <summary>
        /// 根据订单CODE得到列表
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagListByCode(string code)
        {
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            ObjectQuery<Tb_OrderBag> list = GetOrderBagByCode(code);

            foreach (Tb_OrderBag item in list)
            {
                collection.Add(item);
            }

            return collection;
        }

        #region //组合查询

        private static string GetComString(criteriaOrderBag obj)
        {
            string esql = string.Empty;

            //如果订单CODE不为空
            if (!string.IsNullOrEmpty(obj.ordercode))
            {
                string temp = "%" + obj.ordercode;
                esql += " it.BagCode like {0}";
                esql = string.Format(esql, "'" + temp + "'");
            }
            //订单名称不为空
            if (!string.IsNullOrEmpty(obj.bagName))
            {
                string temp = "'%" + obj.bagName + "%'";
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.BagName like {0}";
                }
                else
                {
                    esql += " it.BagName like {0}";
                }
                esql = string.Format(esql, temp);
            }
            //如果下单开始时间不为空
            if (obj.orderStart != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime >  cast({0} as System.DateTime) ";
                }
                else
                {
                    esql += " it.OrderTime >  cast({0} as System.DateTime) ";
                }

                esql = string.Format(esql, "'" + obj.orderStart + "'");
            }

            //如果下单结束时间不为空
            if (obj.orderEnd != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime <  cast({0} as System.DateTime) ";
                }
                else
                {
                    esql += " it.OrderTime <  cast({0} as System.DateTime) ";
                }

                esql = string.Format(esql, "'" + obj.orderEnd + "'");
            }
            //如果取单开始时间不为空
            if (obj.fetchStart != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.FetchTime >  cast({0} as System.DateTime) ";
                }
                else
                {
                    esql += " it.FetchTime >  cast({0} as System.DateTime) ";
                }
                esql = string.Format(esql, "'" + obj.fetchStart + "'");
            }
            //如果取单结束时间不为空
            if (obj.fetchEnd != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.FetchTime <  cast({0} as System.DateTime) ";
                }
                else
                {
                    esql += " it.FetchTime <  cast({0} as System.DateTime) ";
                }
                esql = string.Format(esql, "'" + obj.fetchEnd + "'");
            }

            //如果加急不为空
            if (obj.priority != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.Priority = {0}";
                }
                else
                {
                    esql += " it.Priority = {0}";
                }
                esql = string.Format(esql, obj.priority);
            }

            //生产状态不为空
            if (obj.status != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.Status = {0}";
                }
                else
                {
                    esql += " it.Status = {0}";
                }
                esql = string.Format(esql, obj.status);
            }
            //排除已删除的订单,自动开单还未审核的订单
            if (string.Compare(string.Empty, esql) != 0)
            {
                int delOrder = (int)COM.OrderBagStatus.Deleted;
                int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
                esql += " and it.Status!=" + delOrder + " and it.Status!=" + autoStartOrder;
            }

            return esql;
        }

        #region 根据订单组合条件得到订单列表
        /// <summary>
        /// 根据订单组合条件得到订单列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagListByComCondtion(criteriaOrderBag obj)
        {
            string esql = string.Empty;

            List<Tb_OrderBag> colletion = new List<Tb_OrderBag>();

            esql = GetComString(obj);


            if (string.Compare(esql, string.Empty) != 0)
            {
                ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag.Where(esql);

                colletion.AddRange(objList);
            }

            //如果除了客户查询条件还有其他条件
            if (string.Compare(esql, string.Empty) != 0)
            {
                List<Tb_OrderBag> temp = new List<Tb_OrderBag>();
                if (string.Compare(obj.ctmName, string.Empty) != 0)
                {
                    List<int> bagList = OrderInfoDAL.GetOrderBagIDByCtmName(obj.ctmName);
                    if (colletion.Count > 0)
                    {
                        foreach (var item in colletion)
                        {
                            //记录不存在的
                            if (!bagList.Contains(item.OrderBagId))
                            {
                                temp.Add(item);
                            }
                        }
                        //删除不存在的
                        foreach (var item in temp)
                        {
                            colletion.Remove(item);
                        }
                    }
                }
            }
            else
            {
                if (string.Compare(obj.ctmName, string.Empty) != 0)
                {
                    List<int> bagList = OrderInfoDAL.GetOrderBagIDByCtmName(obj.ctmName);

                    foreach (int bagid in bagList)
                    {
                        colletion.AddRange(GetOrderBagByID(bagid));
                    }
                }
            }

            return colletion;
        }

        /// <summary>
        /// 根据订单组合条件得到订单列表(备份)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag_BK> GetOrderBagListByComCondtion_bk(criteriaOrderBag obj)
        {
            string esql = string.Empty;

            List<Tb_OrderBag_BK> colletion = new List<Tb_OrderBag_BK>();

            esql = GetComString(obj);


            if (string.Compare(esql, string.Empty) != 0)
            {
                ObjectQuery<Tb_OrderBag_BK> objList = DataEntites.Tb_OrderBag_BK.Where(esql);

                colletion.AddRange(objList);
            }

            //如果除了客户查询条件还有其他条件
            if (string.Compare(esql, string.Empty) != 0)
            {
                List<Tb_OrderBag_BK> temp = new List<Tb_OrderBag_BK>();
                if (string.Compare(obj.ctmName, string.Empty) != 0)
                {
                    List<int> bagList = OrderInfoDAL.GetOrderBagIDByCtmName_bk(obj.ctmName);
                    if (colletion.Count > 0)
                    {
                        foreach (var item in colletion)
                        {
                            //记录不存在的
                            if (!bagList.Contains(item.OrderBagId))
                            {
                                temp.Add(item);
                            }
                        }
                        //删除不存在的
                        foreach (var item in temp)
                        {
                            colletion.Remove(item);
                        }
                    }
                }
            }
            else
            {
                if (string.Compare(obj.ctmName, string.Empty) != 0)
                {
                    List<int> bagList = OrderInfoDAL.GetOrderBagIDByCtmName_bk(obj.ctmName);

                    foreach (int bagid in bagList)
                    {
                        colletion.AddRange(GetOrderBagByID_bk(bagid));
                    }
                }
            }

            return colletion;
        }
        #endregion


        #region//分页
        /// <summary>
        /// 带有分页的查询
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        //public static List<Tb_OrderBag> GetOrderBagyComCondtionPage(criteriaOrderBag obj, int start, int end)
        //{
        //    string esql = string.Empty;
        //    List<Tb_OrderBag> colletion = new List<Tb_OrderBag>();

        //    esql = GetComString(obj);

        //    List<Tb_OrderBag> temp = new List<Tb_OrderBag>();
        //    if (string.Compare(obj.ctmName, string.Empty) != 0)
        //    {
        //        List<int> bagList = OrderInfoDAL.GetOrderBagIDByCtmName(obj.ctmName);
        //        if (colletion.Count > 0)
        //        {
        //            foreach (var item in colletion)
        //            {
        //                //记录不存在的
        //                if (!bagList.Contains(item.OrderBagId))
        //                {
        //                    temp.Add(item);
        //                }
        //            }
        //            //删除不存在的
        //            foreach (var item in temp)
        //            {
        //                colletion.Remove(item);
        //            }
        //        }
        //    }
        //    else
        //    { 

        //    }

        //    return colletion;
        //}

        #endregion

        /// <summary>
        /// 根据商户的组合条件得到订单列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagListByShopComCondition(criteriaShop obj)
        {
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            List<int> shopIdList = ShopDAL.GetShopIDByComConidtion(obj);

            foreach (int shopid in shopIdList)
            {
                collection.AddRange(GetOrderBagListByShopID(shopid, obj.orderStart, obj.orderEnd));
            }

            return collection;
        }
        /// <summary>
        /// 根据商户的组合条件得到订单列表(备份)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag_BK> GetOrderBagListByShopComCondition_bk(criteriaShop obj)
        {
            List<Tb_OrderBag_BK> collection = new List<Tb_OrderBag_BK>();

            List<int> shopIdList = ShopDAL.GetShopIDByComConidtion(obj);

            foreach (int shopid in shopIdList)
            {
                collection.AddRange(GetOrderBagListByShopID_bk(shopid, obj.orderStart, obj.orderEnd));
            }

            return collection;
        }


        /// <summary>
        /// 根据订单和产品信息得到查询列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagListByProductComCondition(criteriaProductInfo obj)
        {
            try
            {
                List<Tb_OrderBag> collection = new List<Tb_OrderBag>();


                string esql = string.Empty;

                //如果选择类别
                if (obj.categry != 0 && obj.productid == 0)
                {
                    List<Product> list = GetProductByCate(obj.categry);

                    esql = " it.Tb_Product.ProuctId IN " + ContactCondition(list);

                }
                //产品名称
                if (obj.productid != 0)
                {
                    esql = " it.Tb_Product.ProuctId = {0}";
                    esql = string.Format(esql, obj.productid);
                }

                if (string.Compare(obj.desc, string.Empty) != 0)
                {
                    string temp = "'%" + obj.desc + "%'";
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Tb_Product.ProductDes like {0}";
                    }
                    else
                    {
                        buFen = " it.Tb_Product.ProductDes like {0}";
                    }
                    buFen = string.Format(buFen, temp);
                    esql += buFen;
                }
                //OrderProductCode
                if (string.Compare(obj.code, string.Empty) != 0)
                {
                    string temp = "'%" + obj.code + "%'";
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " and it.OrderProductCode like {0}";
                    }
                    else
                    {
                        buFen = " it.OrderProductCode like {0}";
                    }
                    buFen = string.Format(buFen, temp);
                    esql += buFen;
                }

                //宽度
                if (obj.width != -1)
                {
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Width = {0}";
                    }
                    else
                    {
                        buFen = " it.Width = {0}";
                    }
                    buFen = string.Format(buFen, obj.width);
                    esql += buFen;
                }
                if (obj.height != -1)
                {
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Height = {0}";
                    }
                    else
                    {
                        buFen = " it.Height = {0}";
                    }
                    buFen = string.Format(buFen, obj.height);
                    esql += buFen;
                }
                //排除已删除的订单
                if (string.Compare(esql, string.Empty) != 0)
                {
                    int delOrder = (int)COM.OrderBagStatus.Deleted;
                    int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
                    esql += " and it.Tb_OrderBag.Status!=" + delOrder + " and it.Tb_OrderBag.Status!=" + autoStartOrder;
                }

                if (string.Compare(esql, string.Empty) == 0)
                {
                    esql = "SELECT value it.OrderProductId FROM orderInfo as it WHERE it.status=-100";   //无查询结果
                    //return collection;
                }
                else
                {
                    esql = "SELECT value it.OrderProductId FROM orderInfo as it WHERE " + esql;
                }
                ObjectQuery<int> orderIDList = DataEntites.CreateQuery<int>(esql).Distinct();

                List<int> orderinfoIDList2 = new List<int>();
                List<int> orderinfoIDList = new List<int>();

                //foreach (var itemID in orderIDList)
                //{
                //    orderinfoIDList.Add(itemID);
                //}
                if (orderIDList.Count() > 0)
                    orderinfoIDList2 = orderIDList.ToList();

                //根据PhotoCode得到orderInfo
                if (string.Compare(obj.photoCode, string.Empty) != 0)
                {
                    string esql2 = "select value it.orderInfo.OrderProductId from photoInfo as it where it.PhotoBarCode like '%" + obj.photoCode + "%'";
                    ObjectQuery<int> orderId = DataEntites.CreateQuery<int>(esql2).Distinct();
                    if (orderId.Count() > 0)
                    {
                        foreach (int orderProductId in orderId)
                        {
                            if (orderinfoIDList2.Count > 0)
                            {
                                if (orderinfoIDList2.Contains(orderProductId))
                                {
                                    orderinfoIDList.Add(orderProductId);
                                }
                            }
                            else
                            {
                                orderinfoIDList.Add(orderProductId);
                            }
                        }
                    }
                    else
                    {
                        orderinfoIDList = orderinfoIDList2;
                    }
                }
                else
                {
                    orderinfoIDList = orderinfoIDList2;
                }

                List<int> orderbagIDList = new List<int>();

                foreach (var orderinfoid in orderinfoIDList)
                {
                    ObjectQuery<int> orderbagid = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.Tb_OrderBag.OrderBagId FROM orderInfo AS it WHERE it.OrderProductId = {0}", orderinfoid)).Distinct();

                    List<int> orderbgs = orderbagid.ToList();

                    foreach (int item in orderbgs)
                    {
                        if (!orderbagIDList.Contains(item))
                            orderbagIDList.Add(item);
                    }
                }


                foreach (var bagid in orderbagIDList)
                {
                    collection.AddRange(GetOrderBagByID(bagid));
                }

                return collection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据订单和产品信息得到查询列表(备份)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag_BK> GetOrderBagListByProductComCondition_bk(criteriaProductInfo obj)
        {
            try
            {
                List<Tb_OrderBag_BK> collection = new List<Tb_OrderBag_BK>();


                string esql = string.Empty;

                //如果选择类别
                if (obj.categry != 0 && obj.productid == 0)
                {
                    List<Product> list = GetProductByCate(obj.categry);

                    esql = " it.Tb_Product.ProuctId IN " + ContactCondition(list);

                }
                //产品名称
                if (obj.productid != 0)
                {
                    esql = " it.Tb_Product.ProuctId = {0}";
                    esql = string.Format(esql, obj.productid);
                }

                if (string.Compare(obj.desc, string.Empty) != 0)
                {
                    string temp = "'%" + obj.desc + "%'";
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Tb_Product.ProductDes like {0}";
                    }
                    else
                    {
                        buFen = " it.Tb_Product.ProductDes like {0}";
                    }
                    buFen = string.Format(buFen, temp);
                    esql += buFen;
                }
                //OrderProductCode
                if (string.Compare(obj.code, string.Empty) != 0)
                {
                    string temp = "'%" + obj.code + "%'";
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " and it.OrderProductCode like {0}";
                    }
                    else
                    {
                        buFen = " it.OrderProductCode like {0}";
                    }
                    buFen = string.Format(buFen, temp);
                    esql += buFen;
                }

                //宽度
                if (obj.width != -1)
                {
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Width = {0}";
                    }
                    else
                    {
                        buFen = " it.Width = {0}";
                    }
                    buFen = string.Format(buFen, obj.width);
                    esql += buFen;
                }
                if (obj.height != -1)
                {
                    string buFen = "";
                    if (string.Compare(esql, string.Empty) != 0)
                    {
                        buFen = " AND it.Height = {0}";
                    }
                    else
                    {
                        buFen = " it.Height = {0}";
                    }
                    buFen = string.Format(buFen, obj.height);
                    esql += buFen;
                }
                //排除已删除的订单
                if (string.Compare(esql, string.Empty) != 0)
                {
                    int delOrder = (int)COM.OrderBagStatus.Deleted;
                    int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
                    esql += " and it.Tb_OrderBag_bk.Status!=" + delOrder + " and it.Tb_OrderBag_bk.Status!=" + autoStartOrder;
                }

                if (string.Compare(esql, string.Empty) == 0)
                {
                    esql = "SELECT value it.OrderProductId FROM orderInfo_bk as it WHERE it.status=-100";   //无查询结果
                    //return collection;
                }
                else
                {
                    esql = "SELECT value it.OrderProductId FROM orderInfo_bk as it WHERE " + esql;
                }
                ObjectQuery<int> orderIDList = DataEntites.CreateQuery<int>(esql).Distinct();

                List<int> orderinfoIDList2 = new List<int>();
                List<int> orderinfoIDList = new List<int>();

                //foreach (var itemID in orderIDList)
                //{
                //    orderinfoIDList.Add(itemID);
                //}
                if (orderIDList.Count() > 0)
                    orderinfoIDList2 = orderIDList.ToList();

                //根据PhotoCode得到orderInfo
                if (string.Compare(obj.photoCode, string.Empty) != 0)
                {
                    string esql2 = "select value it.orderInfo_bk.OrderProductId from photoInfo_bk as it where it.PhotoBarCode like '%" + obj.photoCode + "%'";
                    ObjectQuery<int> orderId = DataEntites.CreateQuery<int>(esql2).Distinct();
                    if (orderId.Count() > 0)
                    {
                        foreach (int orderProductId in orderId)
                        {
                            if (orderinfoIDList2.Count > 0)
                            {
                                if (orderinfoIDList2.Contains(orderProductId))
                                {
                                    orderinfoIDList.Add(orderProductId);
                                }
                            }
                            else
                            {
                                orderinfoIDList.Add(orderProductId);
                            }
                        }
                    }
                    else
                    {
                        orderinfoIDList = orderinfoIDList2;
                    }
                }
                else
                {
                    orderinfoIDList = orderinfoIDList2;
                }

                List<int> orderbagIDList = new List<int>();

                foreach (var orderinfoid in orderinfoIDList)
                {
                    ObjectQuery<int> orderbagid = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.Tb_OrderBag_bk.OrderBagId FROM orderInfo_bk AS it WHERE it.OrderProductId = {0}", orderinfoid)).Distinct();

                    List<int> orderbgs = orderbagid.ToList();

                    foreach (int item in orderbgs)
                    {
                        if (!orderbagIDList.Contains(item))
                            orderbagIDList.Add(item);
                    }
                }


                foreach (var bagid in orderbagIDList)
                {
                    collection.AddRange(GetOrderBagByID_bk(bagid));
                }

                return collection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 组合查询条件
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static string ContactCondition(List<Product> list)
        {
            string sqlStr = "{";

            foreach (var item in list)
            {
                sqlStr += item.ProductID + ",";
            }

            sqlStr = sqlStr.Substring(0, sqlStr.Length - 1);
            sqlStr += "}";

            return sqlStr;
        }


        /// <summary>
        /// 用户组合信息查询条件
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag> GetOrderBagListByUserInfo(UserSearchInfo condition)
        {
            List<Tb_OrderBag> collection = new List<Tb_OrderBag>();

            string esql = string.Empty;

            //下单人过滤条件
            if (condition.OrderUserID != -1)
            {
                esql = string.Format(" it.users.userID = {0}", condition.OrderUserID);
            }
            //根据时间段过滤
            if (condition.Start != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime > cast({0} as System.DateTime)";
                }
                else
                {
                    esql += " it.OrderTime > cast({0} as System.DateTime)";
                }
                esql = string.Format(esql, "'" + condition.Start + "'");
            }
            if (condition.End != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime <cast({0} as System.DateTime)";
                }
                else
                {
                    esql += " it.OrderTime <  cast({0} as System.DateTime)";
                }
                esql = string.Format(esql, "'" + condition.End + "'");
            }
            //排除已删除的订单,自动开单还未审核的单
            if (string.Compare(esql, string.Empty) != 0)
            {
                int delOrder = (int)COM.OrderBagStatus.Deleted;
                int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
                esql += " and it.Status!=" + delOrder + " and it.Status!=" + autoStartOrder;
            }
            if (esql != null && esql.Length > 0)
            {
                ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag.Where(esql);
                collection.AddRange(objList);
            }
            //else       //不能查询全部
            //{
            //    ObjectQuery<Tb_OrderBag> objList = DataEntites.Tb_OrderBag;
            //    collection.AddRange(objList);
            //}


            //根据调色，设计，修片来过滤结果
            esql = string.Empty;
            //设计
            if (condition.DesignUserID != -1)
            {
                esql = string.Format(" it.userid = {0} and it.tb_Tech.TechId = 3", condition.DesignUserID);
            }
            //修片
            if (condition.ModifyUserID != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += string.Format(" and it.userid = {0} and it.tb_Tech.TechId = 2", condition.ModifyUserID);
                }
                else
                {
                    esql += string.Format(" it.userid = {0} and it.tb_Tech.TechId = 2", condition.ModifyUserID);
                }
            }
            //调色
            if (condition.ChangeColorUserID != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += string.Format(" and it.userid = {0} and it.tb_Tech.TechId = 4", condition.ChangeColorUserID);
                }
                else
                {
                    esql += string.Format(" it.userid = {0} and it.tb_Tech.TechId = 4", condition.ChangeColorUserID);
                }
            }

            if (string.Compare(esql, string.Empty) != 0)
            {
                List<int> productList = new List<int>();
                ObjectQuery<DbDataRecord> objs = DataEntites.CreateQuery<DbDataRecord>("SELECT it.orderInfo.OrderProductId FROM WorkOrderFlowList as it WHERE " + esql).Distinct();

                foreach (var obj in objs)
                {
                    productList.Add((int)obj[0]);

                }
                if (productList.Count > 0)
                {

                    List<int> bagList = new List<int>();

                    foreach (int infoid in productList)
                    {
                        ObjectQuery<DbDataRecord> bagids = DataEntites.CreateQuery<DbDataRecord>(string.Format("SELECT it.Tb_OrderBag.OrderBagId FROM orderInfo as it WHERE it.OrderProductId = {0}", infoid)).Distinct();

                        foreach (var bagid in bagids)
                        {
                            if (!bagList.Contains((int)bagid[0]))
                            {
                                bagList.Add((int)bagid[0]);
                            }
                        }
                    }

                    if (bagList.Count > 0)
                    {
                        if (collection.Count > 0)
                        {
                            List<Tb_OrderBag> deleteList = new List<Tb_OrderBag>();
                            foreach (var item in collection)
                            {
                                if (!bagList.Contains(item.OrderBagId))
                                {
                                    deleteList.Add(item);
                                }
                            }

                            foreach (var item in deleteList)
                            {
                                collection.Remove(item);
                            }
                        }
                        else
                        {
                            foreach (int orderId in bagList)
                            {
                                collection.AddRange(OrderBagDAL.GetOrderBagByID(orderId));
                            }
                        }
                    }
                }
            }
            return collection;
        }


        /// <summary>
        /// 用户组合信息查询条件(备份)
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<Tb_OrderBag_BK> GetOrderBagListByUserInfo_bk(UserSearchInfo condition)
        {
            List<Tb_OrderBag_BK> collection = new List<Tb_OrderBag_BK>();

            string esql = string.Empty;

            //下单人过滤条件
            if (condition.OrderUserID != -1)
            {
                esql = string.Format(" it.users.userID = {0}", condition.OrderUserID);
            }
            //根据时间段过滤
            if (condition.Start != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime > cast({0} as System.DateTime)";
                }
                else
                {
                    esql += " it.OrderTime > cast({0} as System.DateTime)";
                }
                esql = string.Format(esql, "'" + condition.Start + "'");
            }
            if (condition.End != DateTime.MinValue)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += " AND it.OrderTime <cast({0} as System.DateTime)";
                }
                else
                {
                    esql += " it.OrderTime <  cast({0} as System.DateTime)";
                }
                esql = string.Format(esql, "'" + condition.End + "'");
            }
            //排除已删除的订单,自动开单还未审核的单
            if (string.Compare(esql, string.Empty) != 0)
            {
                int delOrder = (int)COM.OrderBagStatus.Deleted;
                int autoStartOrder = (int)COM.OrderBagStatus.AutoStartOrder;
                esql += " and it.Status!=" + delOrder + " and it.Status!=" + autoStartOrder;
            }
            if (esql != null && esql.Length > 0)
            {
                ObjectQuery<Tb_OrderBag_BK> objList = DataEntites.Tb_OrderBag_BK.Where(esql);
                collection.AddRange(objList);
            }
            //else                  //不能查询全部
            //{
            //    ObjectQuery<Tb_OrderBag_BK> objList = DataEntites.Tb_OrderBag_BK;
            //    collection.AddRange(objList);
            //}


            //根据调色，设计，修片来过滤结果
            esql = string.Empty;
            //设计
            if (condition.DesignUserID != -1)
            {
                esql = string.Format(" it.userid = {0} and it.tb_Tech.TechId = 3", condition.DesignUserID);
            }
            //修片
            if (condition.ModifyUserID != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += string.Format(" and it.userid = {0} and it.tb_Tech.TechId = 2", condition.ModifyUserID);
                }
                else
                {
                    esql += string.Format(" it.userid = {0} and it.tb_Tech.TechId = 2", condition.ModifyUserID);
                }
            }
            //调色
            if (condition.ChangeColorUserID != -1)
            {
                if (string.Compare(esql, string.Empty) != 0)
                {
                    esql += string.Format(" and it.userid = {0} and it.tb_Tech.TechId = 4", condition.ChangeColorUserID);
                }
                else
                {
                    esql += string.Format(" it.userid = {0} and it.tb_Tech.TechId = 4", condition.ChangeColorUserID);
                }
            }

            if (string.Compare(esql, string.Empty) != 0)
            {
                List<int> productList = new List<int>();
                ObjectQuery<DbDataRecord> objs = DataEntites.CreateQuery<DbDataRecord>("SELECT it.orderInfo_BK.OrderProductId FROM WorkOrderFlowList_bk as it WHERE " + esql).Distinct();

                foreach (var obj in objs)
                {
                    productList.Add((int)obj[0]);

                }
                if (productList.Count > 0)
                {

                    List<int> bagList = new List<int>();

                    foreach (int infoid in productList)
                    {
                        ObjectQuery<DbDataRecord> bagids = DataEntites.CreateQuery<DbDataRecord>(string.Format("SELECT it.Tb_OrderBag_BK.OrderBagId FROM orderInfo_BK as it WHERE it.OrderProductId = {0}", infoid)).Distinct();

                        foreach (var bagid in bagids)
                        {
                            if (!bagList.Contains((int)bagid[0]))
                            {
                                bagList.Add((int)bagid[0]);
                            }
                        }
                    }

                    if (bagList.Count > 0)
                    {
                        if (collection.Count > 0)
                        {
                            List<Tb_OrderBag_BK> deleteList = new List<Tb_OrderBag_BK>();
                            foreach (var item in collection)
                            {
                                if (!bagList.Contains(item.OrderBagId))
                                {
                                    deleteList.Add(item);
                                }
                            }

                            foreach (var item in deleteList)
                            {
                                collection.Remove(item);
                            }
                        }
                        else
                        {
                            foreach (int orderId in bagList)
                            {
                                collection.AddRange(OrderBagDAL.GetOrderBagByID_bk(orderId));
                            }
                        }
                    }
                }
            }
            return collection;
        }

        /// <summary>
        /// 得到所有的产品类型
        /// </summary>
        /// <returns></returns>
        public static List<ProductCate> GetAllProductCate()
        {
            List<ProductCate> collection = new List<ProductCate>();

            ObjectQuery<DbDataRecord> records = DataEntites.CreateQuery<DbDataRecord>("SELECT it.ProductCategoryId, it.ProductCategoryName FROM Tb_PorductCategory AS it ");

            ProductCate hold = new ProductCate();
            collection.Add(hold);

            foreach (var record in records)
            {
                ProductCate item = new ProductCate();
                item.ProductCategoryId = (int)record["ProductCategoryId"];
                item.ProductCategoryName = record["ProductCategoryName"] == null ? string.Empty : record["ProductCategoryName"].ToString();

                collection.Add(item);
            }

            return collection;
        }

        /// <summary>
        /// 通过产品的类别ID得到此种类型的所有产品
        /// </summary>
        /// <param name="cateid"></param>
        /// <returns></returns>
        public static List<Product> GetProductByCate(int cateid)
        {
            List<Product> collection = new List<Product>();

            ObjectQuery<DbDataRecord> products = DataEntites.CreateQuery<DbDataRecord>(string.Format("SELECT  it.ProuctId, it.ProductName FROM Tb_Product as it WHERE it.Tb_PorductCategory.ProductCategoryId = {0}", cateid));

            Product hold = new Product();
            collection.Add(hold);
            foreach (var product in products)
            {
                Product item = new Product();
                item.ProductID = (int)product["ProuctId"];
                item.ProductName = (string)product["ProductName"];
                collection.Add(item);
            }

            return collection;
        }


        /// <summary>
        /// 根据产品的名称得到产品的ID
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        internal static List<int> GetProductByName(string productName)
        {
            List<int> collection = new List<int>();

            ObjectQuery<int> productIDlist = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE  it.ProuctId FROM Tb_Product as it WHERE it.ProductName like {0}", "'%" + productName + "%'"));

            foreach (int productID in productIDlist)
            {
                collection.Add(productID);
            }

            return collection;
        }

        #endregion

        /// <summary>
        /// 更新指定订单的加急级别
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="priority"></param>
        public static void UpdatePriority(int orderId, int priority)
        {
            try
            {
                Tb_OrderBag order = DataEntites.Tb_OrderBag.Where(p => p.OrderBagId == orderId).First();
                order.Priority = priority;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新订单及其所有产品的状态(停止与恢复)
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="orderStatus"></param>
        /// <param name="productStatus"></param>
        public static void UpdateOrderAndOrderInfoStatus(int orderId, int orderStatus, int productStatus)
        {
            try
            {
                Tb_OrderBag order = DataEntites.Tb_OrderBag.Where(p => p.OrderBagId == orderId).First();
                List<orderInfo> listProduct = DataEntites.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderId).ToList<orderInfo>();
                foreach (orderInfo info in listProduct)
                {
                    info.status = productStatus;
                }
                order.Status = orderStatus;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 设置产品的状态(停止与恢复)
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="status"></param>
        public static void UpdateProductStatus(int productId, int status)
        {
            try
            {
                orderInfo q = DataEntites.orderInfo.Where(p => p.OrderProductId == productId).First();
                q.status = status;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 单独修改订单的状态
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="status"></param>
        public static void UpdataOrderStatus(int orderId, int status)
        {
            try
            {
                Tb_OrderBag order = DataEntites.Tb_OrderBag.Where(p => p.OrderBagId == orderId).First();
                order.Status = status;
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 插入停止还是恢复产品的备注(以订单为单位)
        /// </summary>
        /// <param name="status"></param>
        public static void InsertStopOrRecorverRemark(int orderId, int status, string remark, int userId)
        {
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    List<orderInfo> listOrder = new List<orderInfo>();
                   // db.Refresh(RefreshMode.StoreWins, db.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderId));
                    int productStatus = 0;          //产品与订单的状态
                    int orderStatus1 = 0;
                    int orderStatus2 = 0;
                    if (status == 0)                //停止
                    {
                        listOrder = db.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderId).ToList<orderInfo>();
                        List<orderInfo> removeInfo = new List<orderInfo>();
                        foreach (orderInfo info in listOrder)
                        {
                            if (info.status == (int)COM.OrderInfoStatus.Stop || info.status == (int)COM.OrderInfoStatus.Completed)      //停止或已完成，就都不能再停止
                                removeInfo.Add(info);
                        }
                        //排除状态已更改的产品
                        foreach (orderInfo info in removeInfo)
                        {
                            listOrder.Remove(info);
                        }
                        productStatus = (int)COM.OrderInfoStatus.Stop;
                        orderStatus1 = (int)COM.OrderBagStatus.OrderBagStop;            //订单内的产品全部停止
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;             //订单内的产品部分停止
                    }
                    else                            //恢复
                    {
                        int stopedStatus = (int)COM.OrderInfoStatus.Stop;
                        listOrder = db.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderId && p.status == stopedStatus).ToList();
                        productStatus = (int)COM.OrderInfoStatus.Doing;
                        orderStatus1 = (int)COM.OrderBagStatus.Doing;
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;
                    }
                   
                        List<CtmOperateRecord> newRecord = new List<CtmOperateRecord>();
                        foreach (orderInfo info in listOrder)
                        {
                            tb_Tech tech = GetCurrentTech(info, status,db);
                            if (tech == null)
                            {
                                continue;
                            }
                            //新增停止(恢复)记录
                            CtmOperateRecord r = new CtmOperateRecord();
                            r.CreateTime = DateTime.Now;
                            r.OperateType = status;
                            r.orderInfo = info;
                            r.Remark = remark;
                            r.tb_Tech = tech;
                            r.users = GetUser(userId,db);
                            newRecord.Add(r);
                            //停止(恢复)当前流程
                            SetCurrentTechStopOrNotStop(info.OrderProductId, status,db);
                            //停止(恢复)产品
                            info.status = productStatus;                                            //产品状态

                            List<New_EmployeePerformance> listNEP = GetNEPByOrderId(info.OrderProductId,db);
                            listNEP.ForEach(p => p.Status = productStatus);//修改人员业绩状态
                            db.SaveChanges();
                            //停止(恢复)订单
                            SetOrderStopOrNotStop(orderId, productStatus, status, orderStatus1, orderStatus2,db);     //订单状态

                            ///吧相关的照片退版
                            info.photoInfo.Load();
                            List<photoInfo> photoList = info.photoInfo.ToList();
                            if (status == 0)//停止
                            {
                                foreach (photoInfo photo in photoList)
                                {
                                    if (photo.status == (int)COM.PhotoStatus.PingBan)
                                    {
                                        //photo.status = (int)PhotoStatus.DistributedPrinter;
                                        photo.picMakeUp.Load();
                                        picMakeUp picMake = photo.picMakeUp.OrderByDescending(p => p.ID).FirstOrDefault();
                                        if (picMake == null)
                                        {
                                            continue;
                                        }
                                        picMake.BoardReference.Load();
                                        picMake.Board.picMakeUp.Load();
                                        List<picMakeUp> picMakeList = picMake.Board.picMakeUp.ToList(); ;

                                        foreach (picMakeUp picMakeother in picMakeList)
                                        {
                                            picMakeother.photoInfoReference.Load();
                                            photoInfo photoPic = picMakeother.photoInfo;

                                            photoPic.status = (int)PhotoStatus.DistributedPrinter;

                                        }


                                        Board deBoard = picMake.Board;
                                        if (deBoard != null)
                                        {
                                            db.DeleteObject(deBoard);
                                        }

                                    }

                                }
                            }

                       
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入停止还是恢复产品的备注(以产品为单位)
        /// </summary>
        /// <param name="status"></param>
        public static void InsertStopOrRecorverRemark2(int productId, int status, string remark, int userId)
        {
            try
            {
                System.Transactions.TransactionOptions options = new System.Transactions.TransactionOptions();
                options.Timeout = new TimeSpan(0, 0, 120);
                using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, options))
                {
                    orderInfo info = GetOrderInfoByProductId(productId);

                    ///吧相关的照片退版
                    info.photoInfo.Load();
                    List<photoInfo> photoList = info.photoInfo.ToList();
                    if (status == 0)//停止
                    {
                        foreach (photoInfo photo in photoList)
                        {
                            if (photo.status == (int)COM.PhotoStatus.PingBan)
                            {
                                //photo.status = (int)PhotoStatus.DistributedPrinter;
                                photo.picMakeUp.Load();
                                picMakeUp picMake = photo.picMakeUp.OrderByDescending(p => p.ID).FirstOrDefault();
                                if (picMake == null)
                                {
                                    continue;
                                }
                                picMake.BoardReference.Load();
                                picMake.Board.picMakeUp.Load();
                                List<picMakeUp> picMakeList = picMake.Board.picMakeUp.ToList(); ;

                                foreach (picMakeUp picMakeother in picMakeList)
                                {
                                    picMakeother.photoInfoReference.Load();
                                    photoInfo photoPic = picMakeother.photoInfo;

                                    photoPic.status = (int)PhotoStatus.DistributedPrinter;

                                }


                                Board deBoard = picMake.Board;
                                if (deBoard != null)
                                {
                                    DataEntites.DeleteObject(deBoard);
                                }

                            }

                        }
                    }
                    tb_Tech tech = GetCurrentTech(info, status);
                    if (tech == null)
                        return;
                    int productStatus = 0;          //产品与订单的状态
                    int orderStatus1 = 0;
                    int orderStatus2 = 0;
                    if (status == 0)
                    {
                        productStatus = (int)COM.OrderInfoStatus.Stop;
                        orderStatus1 = (int)COM.OrderBagStatus.OrderBagStop;            //订单内的产品全部停止
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;             //订单内的产品部分停止
                    }
                    else
                    {
                        productStatus = (int)COM.OrderInfoStatus.Doing;
                        orderStatus1 = (int)COM.OrderBagStatus.Doing;
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;
                    }
                    //停止(恢复)记录
                    CtmOperateRecord newRecord = new CtmOperateRecord();
                    newRecord.CreateTime = DateTime.Now;
                    newRecord.OperateType = status;
                    newRecord.orderInfo = info;
                    newRecord.Remark = remark;
                    newRecord.tb_Tech = tech;
                    newRecord.users = GetUser(userId);
                    //停止(恢复)当前流程
                    SetCurrentTechStopOrNotStop(productId, status);
                    //停止(恢复)产品
                    info.status = productStatus;
                    List<New_EmployeePerformance> listNEP = GetNEPByOrderId(info.OrderProductId);
                    listNEP.ForEach(p => p.Status = productStatus);//修改人员业绩状态
                    DataEntites.SaveChanges();
                    if (!info.Tb_OrderBagReference.IsLoaded)
                        info.Tb_OrderBagReference.Load(MergeOption.OverwriteChanges);
                    //停止(恢复)订单
                    SetOrderStopOrNotStop(info.Tb_OrderBag.OrderBagId, productStatus, status, orderStatus1, orderStatus2);
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 插入停止还是恢复产品的备注(以产品为单位)
        /// </summary>
        /// <param name="status"></param>
        public static void InsertStopOrRecorverRemark3(int productId, int status, string remark, int userId)
        {
            try
            {
              
                using (newSampleYPDBEntities db=new newSampleYPDBEntities())
                {
                    orderInfo info = db.orderInfo.FirstOrDefault(o => o.OrderProductId == productId);

                    ///吧相关的照片退版
                    info.photoInfo.Load();
                    List<photoInfo> photoList = info.photoInfo.ToList();
                    if (status == 0)//停止
                    {
                        foreach (photoInfo photo in photoList)
                        {
                            if (photo.status == (int)COM.PhotoStatus.PingBan)
                            {
                                //photo.status = (int)PhotoStatus.DistributedPrinter;
                                photo.picMakeUp.Load();
                                picMakeUp picMake = photo.picMakeUp.OrderByDescending(p => p.ID).FirstOrDefault();
                                if (picMake == null)
                                {
                                    continue;
                                }
                                picMake.BoardReference.Load();
                                picMake.Board.picMakeUp.Load();
                                List<picMakeUp> picMakeList = picMake.Board.picMakeUp.ToList(); ;

                                foreach (picMakeUp picMakeother in picMakeList)
                                {
                                    picMakeother.photoInfoReference.Load();
                                    photoInfo photoPic = picMakeother.photoInfo;

                                    photoPic.status = (int)PhotoStatus.DistributedPrinter;

                                }


                                Board deBoard = picMake.Board;
                                if (deBoard != null)
                                {
                                    db.DeleteObject(deBoard);
                                }

                            }

                        }
                    }
                    info.WorkOrderFlowList.Load();

                    tb_Tech tech = GetCurrentTech(info, status,db);
                   
                    if (tech == null)
                        return;
                    int productStatus = 0;          //产品与订单的状态
                    int orderStatus1 = 0;
                    int orderStatus2 = 0;
                    if (status == 0)
                    {
                        productStatus = (int)COM.OrderInfoStatus.Stop;
                        orderStatus1 = (int)COM.OrderBagStatus.OrderBagStop;            //订单内的产品全部停止
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;             //订单内的产品部分停止
                    }
                    else
                    {
                        productStatus = (int)COM.OrderInfoStatus.Doing;
                        orderStatus1 = (int)COM.OrderBagStatus.Doing;
                        orderStatus2 = (int)COM.OrderBagStatus.ProductStop;
                    }
                    //停止(恢复)记录
                    CtmOperateRecord newRecord = new CtmOperateRecord();
                    newRecord.CreateTime = DateTime.Now;
                    newRecord.OperateType = status;
                    newRecord.orderInfo = info;
                    newRecord.Remark = remark;
                    newRecord.tb_Tech = tech;
                    newRecord.users = GetUser(userId,db);
                    //停止(恢复)当前流程
                    SetCurrentTechStopOrNotStop(productId, status,db);
                    //停止(恢复)产品
                    info.status = productStatus;
                    List<New_EmployeePerformance> listNEP = GetNEPByOrderId(info.OrderProductId,db);
                    listNEP.ForEach(p => p.Status = productStatus);//修改人员业绩状态
                    db.SaveChanges();
                    if (!info.Tb_OrderBagReference.IsLoaded)
                        info.Tb_OrderBagReference.Load(MergeOption.OverwriteChanges);
                    //停止(恢复)订单
                    SetOrderStopOrNotStop(info.Tb_OrderBag.OrderBagId, productStatus, status, orderStatus1, orderStatus2,db);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 设置订单为停止(非停止)
        /// </summary>
        /// <param name="orderBagId"></param>
        /// <returns></returns>
        public static void SetOrderStopOrNotStop(int orderBagId, int productStatus, int status, int orderStatus1, int orderStatus2)
        {
            try
            {
                List<orderInfo> listInfos = DataEntites.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderBagId).ToList();
                DataEntites.Refresh(RefreshMode.StoreWins, listInfos);
                bool isAllChanged = true;
                int productStop = (int)COM.OrderInfoStatus.Stop;
                if (status == 0)                           //停止
                {
                    foreach (orderInfo info in listInfos)
                    {
                        if (info.status != productStop)
                        {
                            isAllChanged = false;

                            break;
                        }
                    }
                }
                else                                        //恢复
                {
                    foreach (orderInfo info in listInfos)
                    {
                        if (info.status == productStop)
                        {
                            isAllChanged = false;
                            break;
                        }
                    }
                }
                Tb_OrderBag bag = DataEntites.Tb_OrderBag.Where(p => p.OrderBagId == orderBagId).First();
                if (isAllChanged == true)               //订单的产品状态全部为停止(非停止)
                {
                    bag.Status = orderStatus1;              //全部停止(恢复)
                }
                else
                {
                    bag.Status = orderStatus2;              //部分停止
                }
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void SetOrderStopOrNotStop(int orderBagId, int productStatus, int status, int orderStatus1, int orderStatus2,newSampleYPDBEntities db)
        {
            try
            {
                List<orderInfo> listInfos = db.orderInfo.Where(p => p.Tb_OrderBag.OrderBagId == orderBagId).ToList();
               
                bool isAllChanged = true;
                int productStop = (int)COM.OrderInfoStatus.Stop;
                if (status == 0)                           //停止
                {
                    foreach (orderInfo info in listInfos)
                    {
                        if (info.status != productStop)
                        {
                            isAllChanged = false;

                            break;
                        }
                    }
                }
                else                                        //恢复
                {
                    foreach (orderInfo info in listInfos)
                    {
                        if (info.status == productStop)
                        {
                            isAllChanged = false;
                            break;
                        }
                    }
                }
                Tb_OrderBag bag = db.Tb_OrderBag.Where(p => p.OrderBagId == orderBagId).First();
                if (isAllChanged == true)               //订单的产品状态全部为停止(非停止)
                {
                    bag.Status = orderStatus1;              //全部停止(恢复)
                }
                else
                {
                    bag.Status = orderStatus2;              //部分停止
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置当前流程的状态
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <param name="status"></param>
        public static void SetCurrentTechStopOrNotStop(int orderProductId, int status)
        {
            try
            {
                int status1 = (int)COM.OrderFlowListStatus.Useable;
                int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
                int flowStop = (int)COM.OrderFlowListStatus.UnProduct;
                if (status == 0)                //停止
                {
                    WorkOrderFlowList list = DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == orderProductId && (p.Status == status1 || p.Status == status2)).FirstOrDefault();
                    List<New_EmployeePerformance> nep = DataEntites.New_EmployeePerformance.Where(p => p.OrderproductId == orderProductId).ToList();
                    nep.ForEach(p => p.Status = (int)OrderInfoStatus.Stop);
                   
                   

                    if (list != null)
                    {
                        list.Status = flowStop;
                        DataEntites.SaveChanges();
                    }
                }
                else                            //恢复
                {
                    WorkOrderFlowList list = DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == orderProductId && p.Status == flowStop).FirstOrDefault();
                    List<New_EmployeePerformance> nep = DataEntites.New_EmployeePerformance.Where(p => p.OrderproductId == orderProductId).ToList();
                    nep.ForEach(p => p.Status = (int)OrderInfoStatus.Doing);
                    if (list != null)
                    {
                        list.Status = status1;
                        DataEntites.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void SetCurrentTechStopOrNotStop(int orderProductId, int status,newSampleYPDBEntities  db)
        {
            try
            {
                int status1 = (int)COM.OrderFlowListStatus.Useable;
                int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
                int flowStop = (int)COM.OrderFlowListStatus.UnProduct;
                if (status == 0)                //停止
                {
                    WorkOrderFlowList list = db.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == orderProductId && (p.Status == status1 || p.Status == status2)).FirstOrDefault();
                    List<New_EmployeePerformance> nep = db.New_EmployeePerformance.Where(p => p.OrderproductId == orderProductId).ToList();
                    nep.ForEach(p => p.Status = (int)OrderInfoStatus.Stop);



                    if (list != null)
                    {
                        list.Status = flowStop;
                        db.SaveChanges();
                    }
                }
                else                            //恢复
                {
                    WorkOrderFlowList list = db.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == orderProductId && p.Status == flowStop).FirstOrDefault();
                    List<New_EmployeePerformance> nep = db.New_EmployeePerformance.Where(p => p.OrderproductId == orderProductId).ToList();
                    nep.ForEach(p => p.Status = (int)OrderInfoStatus.Doing);
                    if (list != null)
                    {
                        list.Status = status1;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据产品ID获取产品
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static orderInfo GetOrderInfoByProductId(int productId)
        {
            try
            {
                orderInfo info = DataEntites.orderInfo.Where(p => p.OrderProductId == productId).First();
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取产品的当前节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static tb_Tech GetCurrentTech(orderInfo info, int stopStatus)
        {
            try
            {
                ObjectQuery<WorkOrderFlowList> query = null;
                if (stopStatus == 0)           //表示停止
                {
                    int status = (int)COM.OrderFlowListStatus.Useable;
                    int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
                    //WorkOrderFlowList flow = DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == info.OrderProductId && (p.orderInfo.status == status || p.orderInfo.status == status2)).First();
                    query = DataEntites.WorkOrderFlowList.Where("it.orderInfo.OrderProductId=" + info.OrderProductId + " and (it.Status=" + status + " or it.Status=" + status2 + ")");// (p => p.orderInfo == info && (p.Status == status || p.Status == status2));
                }
                else                            //表示恢复
                {
                    int status = (int)COM.OrderFlowListStatus.UnProduct;
                    query = DataEntites.WorkOrderFlowList.Where("it.orderInfo.OrderProductId=" + info.OrderProductId + " and it.Status=" + status);// (p => p.orderInfo == info && (p.Status == status || p.Status == status2));
                }
                List<WorkOrderFlowList> list = query.ToList<WorkOrderFlowList>();
                if (list == null || list.Count == 0)
                    return null;
                return list.First().tb_Tech;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取产品的当前节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static tb_Tech GetCurrentTech(orderInfo info, int stopStatus,newSampleYPDBEntities db)
        {
            try
            {
                //ObjectQuery<WorkOrderFlowList> query = null;
                //if (stopStatus == 0)           //表示停止
                //{
                //    int status = (int)COM.OrderFlowListStatus.Useable;
                //    int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
                //    //WorkOrderFlowList flow = DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == info.OrderProductId && (p.orderInfo.status == status || p.orderInfo.status == status2)).First();
                //    query = db.WorkOrderFlowList.Where("it.orderInfo.OrderProductId=" + info.OrderProductId + " and (it.Status=" + status + " or it.Status=" + status2 + ")");// (p => p.orderInfo == info && (p.Status == status || p.Status == status2));
                //}
                //else                            //表示恢复
                //{
                //    int status = (int)COM.OrderFlowListStatus.UnProduct;
                //    query = db.WorkOrderFlowList.Where("it.orderInfo.OrderProductId=" + info.OrderProductId + " and it.Status=" + status);// (p => p.orderInfo == info && (p.Status == status || p.Status == status2));
                //}
                //List<WorkOrderFlowList> list = query.ToList<WorkOrderFlowList>();
                //if (list == null || list.Count == 0)
                //    return null;
                //return list.First().tb_Tech;
                WorkOrderFlowList curWfl = new WorkOrderFlowList();
                if (stopStatus == 0)           //表示停止
                {
                    int status = (int)COM.OrderFlowListStatus.Useable;
                    int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
                    curWfl = db.WorkOrderFlowList.FirstOrDefault(w => (w.Status == status || w.Status == status2) &&w.orderInfo.OrderProductId==info.OrderProductId);
                }
                else                            //表示恢复
                {
                    int status = (int)COM.OrderFlowListStatus.UnProduct;
                    curWfl = db.WorkOrderFlowList.FirstOrDefault(w => w.Status == 1&&w.orderInfo.OrderProductId==info.OrderProductId) ;
                    if (curWfl == null)
                    {
                        curWfl = db.WorkOrderFlowList.FirstOrDefault(w => w.Status == status && w.orderInfo.OrderProductId == info.OrderProductId);
 
                    }
                }
                if (curWfl != null)
                {
                    curWfl.tb_TechReference.Load();
                    return curWfl.tb_Tech;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取操作员
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static users GetUser(int userId)
        {
            try
            {
                users user = DataEntites.users.Where(p => p.userID == userId).First();
                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取操作员
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static users GetUser(int userId,newSampleYPDBEntities db)
        {
            try
            {
                users user = db.users.Where(p => p.userID == userId).First();
                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除垃圾订单
        /// </summary>
        /// <param name="userId"></param>
        public static void DeleteTempOrder(int userId)
        {
            try
            {
                List<Tb_OrderBag> orders = DataEntites.Tb_OrderBag.Where(p => p.users.userID == userId && p.Status == 100).ToList<Tb_OrderBag>();
                foreach (Tb_OrderBag bag in orders)
                {
                    DataEntites.DeleteObject(bag);
                }
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个订单下有多少个产品
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static int GetProductNumInOrder(int orderId)
        {
            try
            {
                string esql = "select value count(it.OrderProductId) from orderInfo as it where it.Tb_OrderBag.OrderBagId=" + orderId;
                int q = DataEntites.CreateQuery<int>(esql).First();
                return q;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取指定产品的基本信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static orderInfo GetStyleProduct(int productId)
        {
            try
            {
                orderInfo info = DataEntites.orderInfo.Where(p => p.OrderProductId == productId).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取指定产品的基本信息(备份)
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static orderInfo_BK GetStyleProduct_bk(int productId)
        {
            try
            {
                orderInfo_BK info = DataEntites.orderInfo_BK.Where(p => p.OrderProductId == productId).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据orderId获取Order
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static Tb_OrderBag GetOrderByOrderId(int orderId)
        {
            try
            {
              
                Tb_OrderBag order = DataEntites.Tb_OrderBag.Where(p => p.OrderBagId == orderId).FirstOrDefault();
                DataEntites.Refresh(RefreshMode.StoreWins, order);
                return order;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据orderId获取Order
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public static List<New_EmployeePerformance> GetNEPByOrderId(int inProductid)
        {
            try
            {

                List<New_EmployeePerformance>rtnList = DataEntites.New_EmployeePerformance.Where(p => p.OrderproductId == inProductid).ToList();
                //DataEntites.Refresh(RefreshMode.StoreWins, order);
                return rtnList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static List<New_EmployeePerformance> GetNEPByOrderId(int inProductid,newSampleYPDBEntities db)
        {
            try
            {

                List<New_EmployeePerformance> rtnList = db.New_EmployeePerformance.Where(p => p.OrderproductId == inProductid).ToList();
                //DataEntites.Refresh(RefreshMode.StoreWins, order);
                return rtnList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void DelOrderBag(Tb_OrderBag bag)
        {
            DataEntites.DeleteObject(bag);
        }

        #region 刷新
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="obj"></param>
        public static void RefreshObject(object obj)
        {
            try
            {
                DataEntites.Refresh(RefreshMode.StoreWins, obj);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int GetProductNum(string Sql)
        {

            string conStr = DALBase.ConnStr;
            using (SqlConnection con = new SqlConnection(conStr))
            {

                SqlCommand cmd = new SqlCommand(Sql, con);
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                con.Open();
                adapter.Fill(dt);

                return Convert.ToInt32(dt.Rows[0][0]);
            }
        }
        #endregion
    }
}
